package gui;

import gui.actions.ConnectAction;
import gui.actions.DisconnectAction;
import gui.actions.ExitAction;
import gui.actions.ReadLogAction;
import gui.views.LoggingView;
import gui.views.SettingsView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Move all GUI-related stuff in VerappDesktopModel here.
 * 
 * @author Philip
 * 
 */
public class Frame extends JFrame implements ActionListener{

	public static final int WIDTH = 900;
	public static final int HEIGHT = (int)(WIDTH * 0.75);
	//Ratio of frame to be occupied by chart
	public static final double WEIGHT = 0.75;

	private static final long serialVersionUID = 1L;

	private final JPanel cards;
	private final CardLayout cl;
	private final LoggingView lw;

	private JMenuItem logging;
	private JMenuItem settings;
	private JMenuItem disconnectItem;
	private JMenuItem connectItem;

	public Frame() {
		super("Verapp Desktop");
		// For windows look. Much better.
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Failed to initialize GUI, exiting.");
			e.printStackTrace();
			System.exit(1);
		}

		setSize(WIDTH, HEIGHT);
		setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

		setResizable(false);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		setJMenuBar(createMenuBar());

		// Create the panel that contains the "cards".
		cards = new JPanel(new CardLayout());
		lw = new LoggingView();
		cards.add(lw, "Logging");
		cards.add(new SettingsView(), "Settings");

		// Create and set up the content pane.
		cl = (CardLayout) (cards.getLayout());
		// Display the window.
		getContentPane().add(cards);
		pack();
		setVisible(true);
	}

	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("File");

		JMenu viewMenu = new JMenu("Views");

		logging = new JMenuItem("Logging");
		settings = new JMenuItem("Settings");

		logging.addActionListener(this);
		settings.addActionListener(this);

		viewMenu.add(logging);
		viewMenu.add(settings);
		connectItem = new JMenuItem(new ConnectAction());
		disconnectItem = new JMenuItem(new DisconnectAction());
		disconnectItem.getAction().setEnabled(false);
		menu.add(connectItem);
		menu.add(disconnectItem);
		menu.add(new JMenuItem(new ReadLogAction()));
		menu.addSeparator();
		menu.add(viewMenu);
		menu.addSeparator();
		menu.add(new JMenuItem(new ExitAction()));

		menuBar.add(menu);
		return menuBar;
	}

	// TODO - This should be done using the Actions of the menuitems instead
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == settings) {
			cl.show(cards, "Settings");

		} else if (e.getSource() == logging) {
			cl.show(cards, "Logging");
		}
	}

	//Change gui to the specified connected state
	/**
     * Changes the GUI to conform with the current state of web-connection.
     * @param connected New connection status.
     */
    public void setConnected(boolean connected) {
        connectItem.getAction().setEnabled(!connected);
        disconnectItem.getAction().setEnabled(connected);
        update();
    }

	public void connectionFailed() {
		JOptionPane.showMessageDialog(this, "Connection failed, try again.");
		connectItem.getAction().setEnabled(true);
		disconnectItem.getAction().setEnabled(false);
		update();
	}

	public void update() {
		lw.update();
	}
}
